<?php
include_once '../util/arrayutil.php';
class Customer {

    private $id;
    private $firstName;
    private $lastName;
    private $email;
    private $phone;
    private $address;

    public function setId($_id) {
        $this->id = $_id;
    }

    public function getId() {
        return $this->id;
    }

    public function setFirstName($_name) {
        $this->firstName = $_name;
    }

    public function getFirstName() {
        return $this->firstName;
    }

    public function setLastName($_name) {
        $this->lastName = $_name;
    }

    public function getLastName() {
        return $this->lastName;
    }

    public function setEmail($_email) {
        $this->email = $_email;
    }

    public function getEmail() {
        return $this->email;
    }

    public function setPhone($_phone) {
        $this->phone = $_phone;
    }

    public function getPhone() {
        return $this->phone;
    }

    public function setAddress($_address) {
        $this->address = $_address;
    }

    public function getAddress() {
        return $this->address;
    }

    public function save() {
        mysql_query("insert into customer(first_name,last_name,email,phone,address) 
                values('$this->firstName','$this->lastName','$this->enail','$this->phone','$this->address')");
    }

    public function delete() {
        mysql_query("delete from customer where id=$this->id");
    }

    public static function load($id) {
        $result = mysql_query("select id,first_name,last_name,email,phone,address from customer where id=$id")
                or die(mysql_error());
        $row = mysql_fetch_row($result);
        $customer = new Customer();
        $customer->id = $row[0];
        $customer->firstName = $row[1];
        $customer->lastName = $row[2];
        $customer->email = $row[3];
        $customer->phone = $row[4];
        $customer->address = $row[5];
        return $customer;
    }

    public static function loads() {
        $result = mysql_query("select id,first_name,last_name,email,phone,address from customer");
        $customers = array();
        while ($row = mysql_fetch_array($result)) {
            $customer = new Customer();
            $customer->id = $row[0];
            $customer->firstName = $row[1];
            $customer->lastName = $row[2];
            $customer->email = $row[3];
            $customer->phone = $row[4];
            $customer->address = $row[5];
            array_push($customers, $customer);
        }
        return $customers;
    }

    public static function loadsForJSON() {
        return json_encode(ArrayUtil::arrayOfObjectToArray(Customer::loads()));
    }

    public static function loadForJSON($id) {
        return json_encode(ArrayUtil::arrayOfObjectToArray(Customer::load($id)));
    }

}

?>
